package com.bootdo.company.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bootdo.common.constant.RoleConstant;
import com.bootdo.company.domain.CompanyDO;
import com.bootdo.company.service.CompanyService;
import com.bootdo.front.service.PostResumeService;
import com.bootdo.front.vo.JobResultVo;
import com.bootdo.front.vo.Result;
import com.bootdo.system.controller.StatsController;
import com.bootdo.system.domain.JobIndustry;
import com.bootdo.system.domain.UserDO;
import com.bootdo.utils.ResultUtil;
import com.bootdo.utils.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import com.bootdo.company.domain.PostDO;
import com.bootdo.company.service.PostService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import sun.reflect.annotation.ExceptionProxy;

/**
 * 岗位表
 * 
 * @author wudl
 * @email 460030103@qq.com
 * @date 2020-02-19 15:40:05
 */
@Controller
@RequestMapping("/company/post")
public class PostControllers {
	@Autowired
	private PostService postService;
	@Autowired
	private CompanyService companyService;
	@Autowired
    private PostResumeService postResumeService;
	
	@GetMapping()
	@RequiresPermissions("company:post:post")
	String Post(){
	    return "company/post/post";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("company:post:post")
	public PageUtils list(@RequestParam Map<String, Object> params){
		List<PostDO> postList = new ArrayList<>();
		//判断当前用户角色类型，超级管理员可查看所有，企业用户查看本企业数据
		Subject subject = SecurityUtils.getSubject();
		UserDO user = (UserDO)subject.getPrincipal();

		if(user == null || Long.compare(RoleConstant.APPLICANT_ROLE,user.getRoleType()) == 0){
			PageUtils pageUtils = new PageUtils(postList, 0);
			return pageUtils;
		}

		if(Long.compare(RoleConstant.COMPANY_ROLE,user.getRoleType()) == 0){
			params.put("userId",user.getUserId());
		}

		//查询列表数据
        Query query = new Query(params);
		postList = postService.list(query);
		int total = postService.count(query);

		PageUtils pageUtils = new PageUtils(postList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("company:post:add")
	String add(){
	    return "company/post/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("company:post:edit")
	String edit(@PathVariable("id") Long id,Model model){
		PostDO post = postService.get(id);
		model.addAttribute("post", post);
	    return "company/post/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("company:post:add")
	public R save( PostDO post)   {
		try{
			if(postService.save(post)>0){
				return R.ok();
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("company:post:edit")
	public R update( PostDO post){
		postService.update(post);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("company:post:remove")
	public R remove( Long id){
		if(postService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("company:post:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		postService.batchRemove(ids);
		return R.ok();
	}

	@GetMapping("/isCompanyInfo")
	@ResponseBody
	ResultVO<Object> isCompanyInfo() {
		Map<String,Boolean> returnMap = new HashedMap();
		boolean isCompanyInfo = false;
		Subject subject = SecurityUtils.getSubject();
		UserDO user = (UserDO)subject.getPrincipal();
		Long userId = user.getUserId();
		CompanyDO byUserId = companyService.getByUserId(userId);
		if(null!=byUserId){
			isCompanyInfo = true;
		}
		returnMap.put("isCompanyInfo",isCompanyInfo);
		return ResultUtil.success(returnMap);
	}

	@GetMapping("/statPostByIndustry")
	@ResponseBody
	ResultVO<Object> statPostByIndustry() {
		return ResultUtil.success(postService.statPostByIndustry());
	}

	@PostMapping("/search")
	@ResponseBody
	ResultVO<Object> search(@RequestBody Map<String, Object> map) {
		if (map.get("pageNumber") != null && map.get("pageSize") != null) {
			com.github.pagehelper.Page<?> pageHelper  = PageHelper.startPage((int) map.get("pageNumber"), (int) map.get("pageSize"));
			postService.search(map);
			PageInfo<?> pageInfo = pageHelper.toPageInfo();
			return ResultUtil.success(pageInfo);
		}else{
			return ResultUtil.success(postService.search(map));
		}
	}


	/**
	 * 通过职位id查找职位信息
	 *
	 * @param id
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getDetail/{id}")
	public Result getDetail(@PathVariable("id") Long id) {
		JobResultVo jobResultVo = new JobResultVo();
		PostDO postDO = postService.get(id);
		if(postDO != null){
			CompanyDO companyDO = companyService.getByPostId(id);
            JobIndustry getJobIndustry = postService.getJobIndustry(postDO.getIndustryNo());
			jobResultVo.setCompany(companyDO);
			jobResultVo.setPost(postDO);
			jobResultVo.setJobIndustry(getJobIndustry);

			Subject subject = SecurityUtils.getSubject();
			UserDO  userDO = (UserDO)subject.getPrincipal();
			String name = "王**";
			String jobName = postDO.getJobName();
			if(userDO!=null){
				name = userDO.getUsername();
			}
			StatsController.addVistPost(name,jobName);
		}


		return Result.ok(jobResultVo);
	}

    @PostMapping("/companyScanResume")
    @ResponseBody
    ResultVO<Object> companyScanResume(@RequestBody Map<String, Object> map) {
        if (map.get("pageNumber") != null && map.get("pageSize") != null) {
            com.github.pagehelper.Page<?> pageHelper  = PageHelper.startPage((int) map.get("pageNumber"), (int) map.get("pageSize"));
            postResumeService.companyScanResume(map);
            PageInfo<?> pageInfo = pageHelper.toPageInfo();
            return ResultUtil.success(pageInfo);
        }else{
            return ResultUtil.success(postResumeService.companyScanResume(map));
        }
    }

	@PostMapping("/scanResume")
	@ResponseBody
	ResultVO<Object> scanResume(@RequestBody Map<String, Object> map) {
		if (map.get("pageNumber") != null && map.get("pageSize") != null) {
			com.github.pagehelper.Page<?> pageHelper  = PageHelper.startPage((int) map.get("pageNumber"), (int) map.get("pageSize"));
			postResumeService.scanResume(map);
			PageInfo<?> pageInfo = pageHelper.toPageInfo();
			return ResultUtil.success(pageInfo);
		}else{
			return ResultUtil.success(postResumeService.scanResume(map));
		}
	}

}
